package cst.neu.yuejian_31.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import java.util.List;

import cst.neu.yuejian_31.Common.Constant;
import cst.neu.yuejian_31.R;
import cst.neu.yuejian_31.adapter.CommentAdapter;
import cst.neu.yuejian_31.bean.AddCommentBean;
import cst.neu.yuejian_31.bean.CommentBean;
import cst.neu.yuejian_31.iface.AddCommentListener;
import cst.neu.yuejian_31.iface.CommentListner;
import cst.neu.yuejian_31.model.AddCommentModel;
import cst.neu.yuejian_31.model.CommentModel;

public class PostDetailActivity extends BaseActivity implements View.OnClickListener,AddCommentListener {
    private RecyclerView recyclerView;
    private RecyclerView.LayoutManager layoutManager;
    private CommentAdapter commentAdapter;
    private List<CommentBean> list;
    private View view=null;

    private TextView userName,postIntro,postTime;
    private Context context;
    private String postId;
    private ImageView userPhoto,postPic;

    private EditText postComment;
    private Button btn_comment;
    private ImageView btn_return;

    CommentListner commentListner=new CommentListner() {
        @Override
        public void onRespose(List<CommentBean> commentBeen) {
            list=commentBeen;
            commentAdapter.setList(list);
        }

        @Override
        public void onFail(String msg) {

            Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_post_detail);
        initView();
        initDate();
        initEvent();

    }

    private void initDate() {
        Intent intent=getIntent();
        postId=intent.getStringExtra("post_id");
        Toast.makeText(PostDetailActivity.this,postId, Toast.LENGTH_SHORT).show();
        userName.setText(intent.getStringExtra("user_name"));
        postTime.setText(intent.getStringExtra("post_time"));
        postIntro.setText(intent.getStringExtra("post_intro"));
        Picasso.with(context).load(Constant.IMAGE_ADD_SEL_URL+intent.getStringExtra("post_pic")).into(postPic);
        Picasso.with(context).load(Constant.USER_URL+intent.getStringExtra("user_photo")).into(userPhoto);

        CommentModel commentModel=new CommentModel();
        commentModel.getCommentList(postId,commentListner);

        context=PostDetailActivity.this;
        recyclerView= (RecyclerView) findViewById(R.id.recyclerView_comment);
        layoutManager=new LinearLayoutManager(context);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setHasFixedSize(true);
        commentAdapter=new CommentAdapter(context);
        commentAdapter.setList(list);
        recyclerView.setAdapter(commentAdapter);
        recyclerView.addItemDecoration(new DividerItemDecoration(context,LinearLayoutManager.VERTICAL));
    }
    private void initEvent() {
        btn_return.setOnClickListener(this);
        btn_comment.setOnClickListener(this);
    }

    private void initView() {
        userName= (TextView) findViewById(R.id.textView_username_pd);
        postIntro= (TextView) findViewById(R.id.textView_postintro_pd);
        postTime= (TextView) findViewById(R.id.textView_post_time);
        userPhoto= (ImageView) findViewById(R.id.imageView_userphoto_pd);
        postPic= (ImageView) findViewById(R.id.imageView_postpic_pd);
        postComment= (EditText) findViewById(R.id.editText_comment);
        btn_comment= (Button) findViewById(R.id.button_comment);
        btn_return= (ImageView) findViewById(R.id.imageView_return_pd);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.imageView_return_pd:
                finish();
                break;
            case R.id.button_comment:
                addComment();
                break;
        }

    }

    private void addComment() {
        String uid=getUserId();
        AddCommentModel model=new AddCommentModel();
        model.addComment(postId,uid,postComment.getText().toString(),this);
      /*  CommentBean cb=new CommentBean();
        cb.setComment_text(postComment.getText().toString());
        cb.setUser_id(uid);
        list.add(cb);
        commentAdapter.setList(list);
        recyclerView.setAdapter(commentAdapter);*/
    }

    @Override
    public void onSuccess(AddCommentBean addCommentBean) {
        Toast.makeText(context, "评论成功", Toast.LENGTH_SHORT).show();
        CommentModel commentModel=new CommentModel();
        commentModel.getCommentList(postId,commentListner);
    }

    @Override
    public void onFail(String msg) {

        Toast.makeText(context, "评论失败", Toast.LENGTH_SHORT).show();
    }
}
